package com.wjjun.base.ui.viewmodel

import androidx.databinding.Observable
import androidx.databinding.Observable.OnPropertyChangedCallback
import androidx.databinding.PropertyChangeRegistry
import androidx.lifecycle.ViewModel

/**
 * 基础ViewModel
 * viewModel作为界面数据的容器使用
 *
 * author : wangjunjun
 * date   : 2022/5/31
 * desc   :
 */
open class BaseViewModel : ViewModel(), Observable {

    private val callbacks = PropertyChangeRegistry()

    override fun addOnPropertyChangedCallback(callback: OnPropertyChangedCallback) {
        callbacks.add(callback)
    }

    override fun removeOnPropertyChangedCallback(callback: OnPropertyChangedCallback) {
        callbacks.remove(callback)
    }

    /**
     * 通知ui刷新
     */
    fun notifyChange() {
        callbacks.notifyCallbacks(this, 0, null)
    }
}